package com.game.level;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import object.obj2D.bullet.IBullet;
import object.obj3D.enemy.Character;
import object.obj3D.enemy.Player;
import system.Graphics;
import system.KeyBoard;
import system.MyCamera;
import terrain.Terrain;

import com.game.GamePlay;
import com.game.HUD.GUIGame;
import com.game.state.State;

public abstract class Mission extends State{
	//------------------------------------------------------------
	/// Variable declare
	//------------------------------------------------------------
	protected List <Character> 	m_charList;
	protected List <IBullet>	m_enemyBullet;
	protected List <IBullet>	m_playerBullet;
	
	protected int 				m_numOfMachineBullet;
	protected int 				m_numOfFireBullet;
	protected int 				m_numOfMissle;
	
	private int 				m_maxTerminatorHP;
	private int 				m_maxProtossHP;
	private int 				m_maxPlayerHP;
	
	protected int 				m_totalEnemyHP;
	protected Player 			m_player;
	
	protected int 				m_resourceID = 0;
	
	protected int 				m_iMissionStatus = MissionStatus.PLAYING;
	
	protected Terrain 			terrain;
	
	public Mission(){
	}
	
	public void Render(){
		if (!m_bReadyResources)
			return;

		Graphics.GetGraphics().glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		Graphics.GetGraphics().glClearColor(0.4706f, 0.6706f, 0.9137f, 0.8f);
		
		
		Graphics.GetGraphics().glEnable(GL10.GL_DEPTH_TEST);
		for(int i=0; i < m_charList.size(); i++){
			m_charList.get(i).Render();
		}
////
		Graphics.GetGraphics().glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		Graphics.GetGraphics().glEnableClientState(GL10.GL_VERTEX_ARRAY);
		for(int i=0; i< m_enemyBullet.size(); i++){
			m_enemyBullet.get(i).Render();
		}
		
		for(int i = 0; i <m_playerBullet.size(); i++){
			m_playerBullet.get(i).Render();
		}
		GUIGame.Render();

//		terrain.Render(Graphics.GetGraphics());
		// terrain
		// draw gun
	}
	
	public void Update(GamePlay gamePlay){
		if (!m_bReadyResources){
			LoadResource();
			return;
		}
		

		for(int i=0; i < m_charList.size(); i++){
			m_charList.get(i).Update();
		}
		for(int i=0; i< m_enemyBullet.size(); i++){
			m_enemyBullet.get(i).Update();
		}
		for(int i = 0; i <m_playerBullet.size(); i++){
			m_playerBullet.get(i).Update();
		}
		
		GUIGame.Update();
	}
	
	@Override
	public void ProcessKeyboard(GamePlay gamePlay) {
		// TODO Auto-generated method stub
		if (KeyBoard.IsAnyKeyPressed()){
			if (KeyBoard.IsKeyPressed(DK_NUM9)){
				gamePlay.SwitchSubState(GameState.InGameMenu);
			}
			if(KeyBoard.IsKeyPressed(DK_PAD_DOWN))
			{
				//Camera.RotateAlpha(2f);
				MyCamera.rotateAlpha(2f);
			}
			else if(KeyBoard.IsKeyPressed(DK_PAD_UP))
			{
				//Camera.RotateAlpha(-2f);
				MyCamera.rotateAlpha(-2f);
			}
			
			if(KeyBoard.IsKeyPressed(DK_PAD_LEFT))
			{			
				//Camera.RotateBeta(2f);
				MyCamera.rotateBeta(2f);
			}
			else if(KeyBoard.IsKeyPressed(DK_PAD_RIGHT))
			{
				//Camera.RotateBeta(-2f);
				MyCamera.rotateBeta(-2f);
			}
			
			if (KeyBoard.IsKeyPressed(DK_PAD_MIDDLE)){
				if (m_player != null)
					m_playerBullet.add(m_player.Fire(MyCamera.getAlpha(), MyCamera.getBeta(), 0));
			}
		}
	}

	//------------------------------------------------------------
	/// Getter and setter function
	//------------------------------------------------------------
	public void setCharacterList(List<Character> charList) {
		this.m_charList = charList;
	}
	public List<Character> getCharacterList() {
		return m_charList;
	}
	public void setNumOfMachineBullet(int numOfMachineBullet) {
		this.m_numOfMachineBullet = numOfMachineBullet;
	}
	public int getNumOfMachineBullet() {
		return m_numOfMachineBullet;
	}
	public void setNumOfFireBullet(int m_numOfFireBullet) {
		this.m_numOfFireBullet = m_numOfFireBullet;
	}
	public int getNumOfFireBullet() {
		return m_numOfFireBullet;
	}
	public void setNumOfMissle(int m_numOfMissle) {
		this.m_numOfMissle = m_numOfMissle;
	}
	public int getNumOfMissle() {
		return m_numOfMissle;
	}
	public void setEnemyBullet(List <IBullet> m_enemyBullet) {
		this.m_enemyBullet = m_enemyBullet;
	}
	public List <IBullet> getEnemyBullet() {
		return m_enemyBullet;
	}
	public void setPlayerBullet(List <IBullet> m_playerBullet) {
		this.m_playerBullet = m_playerBullet;
	}
	public List <IBullet> getPlayerBullet() {
		return m_playerBullet;
	}
	public void setMaxTerminatorHP(int m_maxTerminatorHP) {
		this.m_maxTerminatorHP = m_maxTerminatorHP;
	}
	public int getMaxTerminatorHP() {
		return m_maxTerminatorHP;
	}
	public void setMaxProtossHP(int m_maxProtossHP) {
		this.m_maxProtossHP = m_maxProtossHP;
	}
	public int getMaxProtossHP() {
		return m_maxProtossHP;
	}
	public void setMaxPlayerHP(int m_maxPlayerHP) {
		this.m_maxPlayerHP = m_maxPlayerHP;
	}
	public int getMaxPlayerHP() {
		return m_maxPlayerHP;
	}
	
	public int getCurrentPlayerHP() {
		return m_player.GetCurrentHP();
	}
	
	
	
	public int GetTotalAliveEnemyHP(){
		int sum = 0;
		for (int i = 0; i < m_charList.size(); ++i){
			sum += m_charList.get(i).GetCurrentHP();
		}
		return sum;
	}
	
	public int GetTotalEnemyHP(){
		return m_totalEnemyHP;
	}
	
	public Player GetPlayer(){
		return m_player;
	}
	
	public abstract void LoadResource();
	
	public int GetStatus(){
		return m_iMissionStatus;
	}
}
